package com.itany.cloud.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.itany.cloud.util.ExceptionUtil;
import com.itany.cloud.util.ResponseResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Author:shixiaojun@itany.com
 * Date:2022/5/31 9:16
 * Version:1.0-SNAPSHOT
 * Description:
 */
@RestController
public class HelloController {

    // value属性:指定当前需要处理的请求是谁
        // 其值是当前的请求名,不要带有/
        // 如果方法没有指定请求名,其值即为空字符串
    // blockHandler属性:处理BlockException的函数名称
    // 其属性对应的是处理的方法名
    // 该方法需要我们自定义
    // 该方法必须满足以下条件
    // 1.方法必须处于当前Controller中
    // 2.方法的返回值与当前方法保持一致
    // 3.方法必须使用public修饰
    // 4.方法的参数必须是BlockException
    @SentinelResource(value = "f1",blockHandler = "handlerException")
    @RequestMapping("/f1")
    public ResponseResult f1(){
        return ResponseResult.success();
    }

    public ResponseResult handlerException(BlockException e){
        return ResponseResult.fail("Sentinel异常,异常为:"+e);
    }

    @SentinelResource(value = "f2",blockHandlerClass = ExceptionUtil.class,blockHandler = "handlerException")
    @RequestMapping("/f2")
    public ResponseResult f2(){
        return ResponseResult.success();
    }

}
